<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2018/5/30
 * Time: 下午1:51
 */

namespace App\Http\Controllers\Api\Pay;


use App\Http\Controllers\Api\Pay\Transformers\PayWithdrawCashTransformer;
use App\Http\Controllers\Controller;
use App\Modules\Balance\Services\BalanceService;
use App\Modules\Pay\Services\PayWithdrawCashService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class PayWithdrawCashController extends Controller
{
    /**
     * @apiDefine WeChatPay 微信支付
     */

    /**
     * @apiVersion 1.0.0
     * @apiDescription 提现申请
     * @api {POST} pay/withdrawCash/add 提现申请
     * @apiName addWithdrawCash
     * @apiGroup WeChatPay
     *
     * @apiParam   {Number}  amount 提现金额.
     * @apiParam   {String}  [remark] 备注说明.
     *
     * @apiSuccess   {Number}  enable_amount 可提现余额.
     * @apiSuccess   {Number}  withdraw_pending_amount 提现中金额.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":
     *         {
     *           "enable_amount": 70,
     *           "withdraw_pending_amount": 1100
     *         }
     *     }
     */
    function addWithdrawCash(Request $request) {
        if($this->isNotLogin()) {
            return response()->error("NOT_LOGIN");
        }

        $amount = $request->has('amount') ? $request->input('amount') : '';
        Log::info('with_amount:'.$amount);
        if(!is_numeric($amount)) {
            return response()->error("PARAM_ERROR");
        }
        if($amount < 100) {
            //单笔提现大于 100 金额的判断 大于1一块钱就可以
            if($amount < 1) {
                return response()->error("WITHDRAW_CASH_AMOUNT");
            }
            if(!in_array($this->getLoginUserId(), explode(',',env('TEST_PAY_UID')))){
                //TODO  暂时取消 限制
                //return response()->error("WITHDRAW_CASH_AMOUNT");
            }
        }
        if($amount > (20000)) {
            //给同一个实名用户付款，单笔单日限额2W/2W
            //取消单笔提现额度限制，微信企业付款到零钱限制
            return response()->error("WITHDRAW_CASH_AMOUNT_MORE");
        }

        $remark = $request->has('remark') ? $request->input('remark') : '';

        //判断用户可提现余额
        $balance = BalanceService::getUserBalance($this->getLoginUserId());
        if(!$balance || $balance->available_balance < $amount) {
            //余额不足
            return response()->error("WITHDRAW_CASH_AMOUNT_INSUFFICIEN");
        }

        //扣除用户可提现余额
        BalanceService::reflect($this->getLoginUserId(), $amount);

        //添加提现流水
        PayWithdrawCashService::addWithdrawCash($this->getLoginUserId(), $amount, $remark);

        //修改可提现总额
        $balance = BalanceService::getUserBalance($this->getLoginUserId());
        $enable_amount = $balance->balance;
        //修改提现中金额
        $withdraw_pending_amount = $amount;

        return response()->success(compact('enable_amount', 'withdraw_pending_amount'));
    }



    /**
     * @apiVersion 1.0.0
     * @apiDescription 提现列表
     * @api {GET} pay/withdrawCash/list 提现列表
     * @apiName listWithdrawCash
     * @apiGroup WeChatPay
     *
     * @apiParam   {Number}  [status] 状态 查看接口 [0:待审核；10:待打款；20:打款中；30:打款成功； 40:打款失败；]
     * @apiParam   {String}  [start_time] 开始时间(可不传)
     * @apiParam   {String}  [end_time] 结束时间(可不传)
     *
     * @apiSuccess   {Number}  id 提现ID
     * @apiSuccess   {Number}  amount 提现金额.
     * @apiSuccess   {Number}  tallage 扣税金额.
     * @apiSuccess   {Number}  pay_amount 打款金额.
     * @apiSuccess   {String}  status 状态.
     * @apiSuccess   {String}  remark 备注.
     * @apiSuccess   {Number}  check_user_id 审核人ID.
     * @apiSuccess   {String}  created_time 提现时间.
     * @apiSuccess   {String}  updated_time 最后更新时间.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data": {
     *                   "list": [
     *                           {
     *                                       "id":1
     *                                       "amount": 330.00,
     *                                       "tallage": "11",
     *                                       "pay_amount": "319.00",
     *                                       "status": "待审核",
     *                                       "remark": null,
     *                                       "check_user_id": null,
     *                                       "check_user_name": null,
     *                                       "created_time": "2017-12-01 16:58:55",
     *                                       "updated_time": "2017-12-01 16:58:59"
     *                               }
     *                       ],
     *                   "meta": {
     *                       "total": 2,
     *                       "per_page": 15,
     *                       "current_page": 1,
     *                       "last_page": 1,
     *                       "next_page_url": "",
     *                       "prev_page_url": ""
     *                   }
     *              }
     *     }
     */
    function listWithdrawCash(Request $request) {
        if($this->isNotLogin()) {
            return response()->error("NOT_LOGIN");
        }

        $start_time = $this->getParameterStartTime($request);
        $end_time = $this->getParameterEndTime($request);
        $statusIn = $request->has('status') ? $request->input('status') : '';

        $searchParams = [
            'uid'=>$this->getLoginUserId(),
            'start_time' => $start_time,
            'end_time' => $end_time,
        ];
        $result = PayWithdrawCashService::getList($searchParams, $statusIn);

        return response()->pagination(new PayWithdrawCashTransformer(),  $result);
    }

}